package appLoader.displayAdmin
{
	import appLoader.I_Loader;
	
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	
	import mx.logging.ILogger;
	import mx.logging.Log;
	import mx.logging.LogEvent;
	import mx.logging.LogEventLevel;
	
	public class DisplayLoaderAdmin extends EventDispatcher implements I_Loader
	{
		
		public static const LOADING:String = "Loading";
		public static const LOAD_COMPLETE:String = "Load Complete";
		public static const LOAD_ITEM_PROGRESS:String = "Load item Progress";
		
		private var _newDiplayItem:DisplayLoaderItem;
		private var _newDisplayLibrary:Array;
		private var _timerProgress:Timer;
		
		private static var logger:ILogger = Log.getLogger("DisplayLoaderAdmin");
		private var _libraryTotalSize:uint;
		private var _partialProgress:int;
		private var _progress:int;
		private var _isLoaded:Boolean;
		private var _generalContainer:MovieClip;
		
		public function DisplayLoaderAdmin()
		{
			logger.log(LogEventLevel.INFO, "DisplayLoaderAdmin");
			_newDisplayLibrary = new Array;
			_timerProgress =new Timer(1);
			_timerProgress.addEventListener(TimerEvent.TIMER, itemLoading);
		}
		
		public function initLoad():void{
			for each (var tempDisplay:DisplayLoaderItem in _newDisplayLibrary) {
				tempDisplay.addEventListener(DisplayLoaderItem.LOAD_COMPLETE, itemLoaded);
				_libraryTotalSize+=tempDisplay.itemSizeKb;
				tempDisplay.initLoad();
				dispatchEvent(new Event(LOADING));
			}
			_timerProgress.start();
		}
		
		protected function itemLoading(event:TimerEvent):void
		{
			_partialProgress = 0;
			progress = 0;
			for each(var tempDisplay:DisplayLoaderItem in _newDisplayLibrary) {
				_partialProgress+=tempDisplay.percent
			}
			
			progress=_partialProgress/_newDisplayLibrary.length;
			if (progress>=100) {
				_timerProgress.stop();
				_timerProgress.reset();
			}
			dispatchEvent(new Event(LOAD_ITEM_PROGRESS));
		}
		
		private function itemLoaded(event:Event):void {
			var displayLoadedCounter:int = 0;
			for each (var tempDisplay:DisplayLoaderItem in _newDisplayLibrary) {
				if (tempDisplay.displayLoaded) {
					displayLoadedCounter++;
				}
				if (displayLoadedCounter==_newDisplayLibrary.length) {
					_isLoaded = true;
					dispatchEvent(new Event(LOAD_COMPLETE));
				}
			}
		}
		
		public function setItem(valueId:String, valueUrl:String):void
		{
			if (valueUrl!=""){
				_newDiplayItem = new DisplayLoaderItem(valueId, valueUrl);
				_newDiplayItem.container=_generalContainer;
			}
			_newDisplayLibrary.push(_newDiplayItem);
		}
				
		public function getItem(value:String):DisplayLoaderItem
		{
			var result:DisplayLoaderItem;
			if (value!=""){
				for each (var tempDisplay:DisplayLoaderItem in _newDisplayLibrary) {
					if (tempDisplay.id == value) {
						result=tempDisplay;
					}
				}
			}
			return result;
		}
		
		public function getLibraryIds():Array {
			var result:Array = new Array;
				for each (var tempDisplay:DisplayLoaderItem in _newDisplayLibrary) {
					result.push(tempDisplay.id);
			}
			return result;
		}
		
		public function get newDisplayLibrary():Array
		{
			return _newDisplayLibrary;
		}

		public function set newDisplayLibrary(value:Array):void
		{
			_newDisplayLibrary = value;
		}

		public function get libraryTotalSize():uint
		{
			return _libraryTotalSize;
		}

		public function set libraryTotalSize(value:uint):void
		{
			_libraryTotalSize = value;
		}

		public function get progress():int
		{
			return _progress;
		}

		public function set progress(value:int):void
		{
			_progress = value;
		}

		public function getPercentLoaded():int {
			return progress;
		}
		
		public function isLoaded():Boolean{
			return _isLoaded;
		}
		
		
		public function get generalContainer():MovieClip
		{
			return _generalContainer;
		}
		
		public function set generalContainer(value:MovieClip):void
		{
			_generalContainer = value;
			for each (var tempDisplay:DisplayLoaderItem in _newDisplayLibrary) {
				tempDisplay.container=_generalContainer;
			}
		}
	}
}